<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$return['role']=$_SESSION['role'];
$return['cbtitle']=$this->credit_type[3]?:'茶币';
if($_RQ['op']=='main'){
	$member=model('core/member')->detail($_SESSION['uid'],array('credit1','credit2','credit3','credit4','credit6','mobile','avatar','nickname'));
	$mymember=pdo_get('jy_nongfee_member',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	$member=array_merge($member,$mymember);
	$daytime = mktime(0,0,0,date('m'),date('d'),date('Y'));
	$member['sifang']=$this->getsum('commission',array('createtime between'=>array($daytime-24*3600,$daytime),'status'=>1,'uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),'jifen');
	$member['sifang1']=$this->getsum('commission',array('type <='=>2,'createtime between'=>array($daytime-24*3600,$daytime),'status'=>1,'uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),'jifen');
	$member['sifang2']=$this->getsum('commission',array('type >'=>2,'createtime between'=>array($daytime-24*3600,$daytime),'status'=>1,'uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),'jifen');
	$return['member']=$member;
	$balance=pdo_get('jy_nongfee_balance',array('status'=>0,'end_time >'=>time(),'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	$balance['end_time']=$balance['end_time']?date('Y-m-d H:i',$balance['end_time']):'';
	$return['balance']=$balance;
	$return['teamsetting']=$this->teamsetting;
	$return['sf_rate']=$this->teamsetting['sf_rate']?:1;
	$balance['lastmoney']=$balance['lastmoney']?:0;
	$tips='
	1、释放值,可以用于'.$this->cd_title.'兑换'.$return['cbtitle'].'；
	2、当前剩余释放值：'.$balance['lastmoney'].'个，剩余'.$this->cd_title.''.$member['credit4'].'个;
	';
	$canliu=$balance['lastmoney']-$member['credit4'];
	if($canliu>0){
		$tips .=$balance['end_time'].'会扣除掉多余的'.$canliu.'个释放值！';
	}
	$return['tips']=$tips;
	$return['params']=array('type'=>0);
	$return['list']=array();
	set_html('title','我的释放值');
	exi($return);
}
if($_RQ['op']=='getlist'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = 15;
	$params=array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']);
	if(!$_RQ['type']){
		$table='jy_nongfee_commission';
	}else{
		$table='jy_nongfee_balance';
	}
    $orderby='createtime desc';
	$total='';
	$return['list']=pdo_getslice($table,$params,array($pindex,$psize),$total,$fields,'',$orderby);	
	foreach($return['list'] as &$v){
		$v['createtime']=date('Y-m-d H:i:s',$v['createtime']);
		if(!$_RQ['type']){
			$v['statustitle']=$this->commission_status[$v['status']];
			$v['typetitle']=$this->commission_money_types[$v['type']];
			$v['fee']=$v['jifen'];
		}else{
			$v['typetitle']=$this->balance_status[$v['status']];
			$v['fee']=$v['money'];
		}
	}
	exi($return);
}
if($_RQ['op']=='getsifang'){
	$this->model('area/commission')->create_balance($_SESSION['uid']);
	$this->model('commission')->create_balance($_SESSION['uid']);
	exi('释放成功！');
}
if($_RQ['op']=='post'){
	$balance=pdo_get('jy_nongfee_balance',array('status'=>0,'end_time >'=>time(),'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	$post=get_postdata();
	if(!$post['fee']){
		exi('请输入兑换'.$this->cd_title.'！','erroe');
	}
	if($balance['lastmoney']<$post['fee']){
		exi('兑换额度不能大于，当前剩余释放值额度！','error');
	}
	$item=model('core/member')->detail($_SESSION['uid'],array('credit1','credit2','credit3','credit4','credit6','mobile','avatar','nickname'));
	if($item['credit4']<$post['fee']){
		exi('当前剩余'.$this->cd_title.'只有'.$item['credit4'].'个！','error');
	}
	$sf_rate=$this->teamsetting['sf_rate']?:1;
	$credit3=$post['fee']/$sf_rate;
	model('core/member')->member_credit($_SESSION['uid'],'credit3',$credit3,true,'jy_nongfee',''.$this->cd_title.'兑换获得');
	model('core/member')->member_credit($_SESSION['uid'],'credit4',$post['fee'],false,'jy_nongfee',''.$this->cd_title.'兑换'.$return['cbtitle']);
	$this->model('commission')->credit4tocredit3($post['fee'],$balance);
	exi('兑换成功！');
}